package cn.initcap.controller;

import cn.initcap.common.ErrorCode;
import cn.initcap.common.Result;
import cn.initcap.entity.OrderInfo;
import cn.initcap.entity.SeckillUser;
import cn.initcap.service.GoodsService;
import cn.initcap.service.OrderService;
import cn.initcap.vo.GoodsVo;
import cn.initcap.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author initcap
 * @date 2018/8/10 PM5:53
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    private OrderService orderService;
    private GoodsService goodsService;

    @Autowired
    public OrderController(OrderService orderService, GoodsService goodsService) {
        this.orderService = orderService;
        this.goodsService = goodsService;
    }

    @RequestMapping("/detail")
    @ResponseBody
    public Result info(SeckillUser user,
                       @RequestParam("orderId") long orderId) {
        if (user == null) {
            return Result.fail(ErrorCode.SESSION_ERROR);
        }
        OrderInfo order = orderService.getOrderById(orderId);
        if (order == null) {
            return Result.fail(ErrorCode.ORDER_NOT_EXIST);
        }
        long goodsId = order.getGoodsId();
        GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVo vo = new OrderDetailVo();
        vo.setOrder(order);
        vo.setGoods(goods);
        return Result.success(vo);
    }

}
